package com.bloomingstars.sqm.sgoudelis.poller;
import java.io.StringReader;
import java.net.UnknownHostException;

import com.opencsv.CSVReader;

/**
 * SQM-LE device class
 * Provides high-level (device level) communication
 * @author Stratos Goudelis
 *
 */
public class SQMLE {
	protected String ip;
	protected int port;
	protected String password;
	protected SQMLEConnector connector;

	protected int protocolVersion;
	protected int modelVersion;
	protected int featureNumber;
	protected int serialNumber;

	protected float magPerSqAS;
	protected long frequency;
	protected long periodCount;
	protected double periodSeconds;
	protected float temperature;

	protected int replyTimeout = 200; 
	
	/**
	 * @param args
	 * @throws Exception
	 */
	/**
	 * @param ip
	 * @param port
	 * @param password
	 */
	public SQMLE(String ip, int port, String password) {
		this.ip = ip;
		this.port = port;
		this.password = password;
	}
	
	/**
	 * @param ip
	 * @param port
	 */
	public SQMLE(String ip, int port) {
		this.ip = ip;
		this.port = port;
		this.password = "";
	}

	/**
	 * @throws Exception
	 */
	public void getReadings() throws Exception, UnknownHostException, SQMLEPasswordException {
		this.connector = new SQMLEConnector(ip, port, password);
		
		this.connector.connect();

		try {
			this.connector.sendPassword();
		} catch (SQMLEException e) {
			throw new SQMLEPasswordException("Error sending password");
		}
		
		Thread.sleep(100);
		
		// take infos
//		String infoReply = "";
//		try {
//			infoReply = this.connector.sendCmd("ix", this.replyTimeout);
//			StringReader reader = new StringReader(infoReply);
//			CSVReader csvreader = new CSVReader(reader);
//			String sampleList[] = csvreader.readNext();
//			reader.close();
//			csvreader.close();
//
//			this.protocolVersion = Integer.parseInt(sampleList[1]);
//			this.modelVersion = Integer.parseInt(sampleList[2]);
//			this.featureNumber = Integer.parseInt(sampleList[3]);
//			this.serialNumber = Integer.parseInt(sampleList[4]);
//		} catch (SQMLEException e) {
//			throw new Exception("Something went wrong sending command ix: "
//					+ e.getMessage());
//		}

		// take initial reading
		String sampleReply = "";
		try {
			sampleReply = this.connector.sendCmd("rx", replyTimeout);
			StringReader reader = new StringReader(sampleReply);
			CSVReader csvreader = new CSVReader(reader);
			String sampleList[] = csvreader.readNext();
			reader.close();
			csvreader.close();

			this.magPerSqAS = Float.parseFloat(sampleList[1].substring(0, sampleList[1].length()-1));
			this.frequency = Long.parseLong(sampleList[2].substring(0, sampleList[1].length()-1));
			this.periodCount = Long.parseLong(sampleList[3].substring(0, sampleList[3].length()-1));
			this.periodSeconds = Double.parseDouble(sampleList[4].substring(0, sampleList[4].length()-1));
			this.temperature = Float.parseFloat(sampleList[5].replace("C", ""));
		} catch (SQMLEException e) {
			throw new Exception("Something went wrong sending command rx: " + e.getMessage());
		}
		this.connector.disconnect();
	}

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public SQMLEConnector getConnector() {
        return connector;
    }

    public void setConnector(SQMLEConnector connector) {
        this.connector = connector;
    }

    public int getProtocolVersion() {
        return protocolVersion;
    }

    public void setProtocolVersion(int protocolVersion) {
        this.protocolVersion = protocolVersion;
    }

    public int getModelVersion() {
        return modelVersion;
    }

    public void setModelVersion(int modelVersion) {
        this.modelVersion = modelVersion;
    }

    public int getFeatureNumber() {
        return featureNumber;
    }

    public void setFeatureNumber(int featureNumber) {
        this.featureNumber = featureNumber;
    }

    public int getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(int serialNumber) {
        this.serialNumber = serialNumber;
    }

    public float getMagPerSqAS() {
        return magPerSqAS;
    }

    public void setMagPerSqAS(float magPerSqAS) {
        this.magPerSqAS = magPerSqAS;
    }

    public long getFrequency() {
        return frequency;
    }

    public void setFrequency(long frequency) {
        this.frequency = frequency;
    }

    public long getPeriodCount() {
        return periodCount;
    }

    public void setPeriodCount(long periodCount) {
        this.periodCount = periodCount;
    }

    public double getPeriodSeconds() {
        return periodSeconds;
    }

    public void setPeriodSeconds(double periodSeconds) {
        this.periodSeconds = periodSeconds;
    }

    public float getTemperature() {
        return temperature;
    }

    public void setTemperature(float temperature) {
        this.temperature = temperature;
    }

    public int getReplyTimeout() {
        return replyTimeout;
    }

    public void setReplyTimeout(int replyTimeout) {
        this.replyTimeout = replyTimeout;
    }

	
}
